package services.user;

import org.apache.commons.lang3.StringUtils;

import services.exception.*;
import services.security.*;
import utils.*;

import dto.user.*;
import models.user.*;
import models.pubobj.*;

public class AniLocalUserRegService extends AniLocalUserService{

	//public user info generate service
	public static void publicUserRegister(LocalUserInfoDto oneLocalUserRegInfo){
		checkUserRegInfo(oneLocalUserRegInfo);
		saveUser(oneLocalUserRegInfo, getUserRoleWithKey(AniUserRole.RoleKeys.ANI_USER.name()));
	}
	//admin user register service
	public static void adminUserRegister(LocalUserInfoDto oneLocalUserRegInfo){
		checkUserRegInfo(oneLocalUserRegInfo);
		saveUser(oneLocalUserRegInfo, getUserRoleWithKey(AniUserRole.RoleKeys.ANI_ADMIN.name()));
	}
	
	//check user register info legality
	protected static void checkUserRegInfo(LocalUserInfoDto userRegInfo) {
		//need catch in controller
		checkRequiredUserRegisterInfo(userRegInfo);
		
		AniPatternUtils.checkEmailFormatLegality(userRegInfo.email);
		AniLocalUserRegService.checkScreenNameLegality(userRegInfo.screenName);
		
		checkUserEmailUnique(userRegInfo.email);
		checkUserScreenNameUnique(userRegInfo.screenName);
	}
	private static void checkRequiredUserRegisterInfo(LocalUserInfoDto localUserRegInfo){
		if(StringUtils.isEmpty(localUserRegInfo.email)){
			throw new AniAuthException("EMAIL_IS_REQUIRED");
		}
		if(StringUtils.isEmpty(localUserRegInfo.md5Pwd)){
			throw new AniAuthException("PWD_IS_REQUIRED");
		}
	}
}

